//
//  MCReportFormsItemView.swift
//  StoreManage
//
//  Created by MC on 2018/8/13.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit
import Charts


class MCReportFormsItemView: UIView {

    // X轴
    public var titles : [String]?
    
    
    // X轴的时间，Y轴的数据
    public var dataTuples : (X:[String],Y:[Double]) = (X:[String](),Y:[Double]()) {
        didSet {
            titles = dataTuples.X
            self.setData(datas: dataTuples.Y)
        }
    }

    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        
        self.addSubview(lineOneView)
        lineOneView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.size.equalTo(CGSize.init(width: 3, height: 16))
            make.top.equalTo(19)
        }
        
        self.addSubview(lineTwoView)
        lineTwoView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.height.equalTo(1)
            make.top.equalTo(44)
        }
        
        
        self.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(lineOneView.snp.right).offset(7)
            make.centerY.equalTo(lineOneView)
        }
        
        self.addSubview(lineThreeView)
        lineThreeView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.height.equalTo(1)
            make.bottom.equalTo(-83)
        }

        
        let width = MCScreenWidth / 3
        
        self.addSubview(leftView)
        leftView.snp.remakeConstraints { (make) ->Void in
            make.size.equalTo(CGSize.init(width: width, height: 75))
            make.top.equalTo(lineThreeView.snp.bottom)
            make.left.equalTo(self)
        }
        
        self.addSubview(centerView)
        centerView.snp.remakeConstraints { (make) ->Void in
            make.size.equalTo(CGSize.init(width: width, height: 75))
            make.top.equalTo(leftView)
            make.left.equalTo(leftView.snp.right)
        }
        
        self.addSubview(rightView)
        rightView.snp.remakeConstraints { (make) ->Void in
            make.size.equalTo(CGSize.init(width: width, height: 75))
            make.top.equalTo(leftView)
            make.left.equalTo(centerView.snp.right)
        }
        
        self.addSubview(chartView)
        chartView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.top.equalTo(lineTwoView.snp.bottom).offset(5)
            make.bottom.equalTo(lineThreeView.snp.top).offset(-5)
        }        
    }
    
    
    fileprivate func setData(datas: [Double]) {
        
        let yVals = (0..<datas.count).map { (i) -> BarChartDataEntry in
            let val = Double(datas[i])
            return BarChartDataEntry(x: Double(i), y: val)
        }

        
        // 无数据的情况
        if yVals.count == 0 {
            chartView.data = nil
            chartView.data?.notifyDataChanged()
            chartView.notifyDataSetChanged()
            chartView.noDataText = "暂无数据，请重新刷新"
            return
        }

        
        
        var str = ""
        if promptLabel.text == "销售额" {
            str = "销售额(元)"
        } else {
            str = "订单量(个)"
        }
        
        

        let set1:BarChartDataSet = BarChartDataSet.init(values: yVals, label: str)
        set1.colors = [MCColor_16(colorStr: "2E99FF")]
        set1.formSize = 8.0
        set1.highlightEnabled = false
        set1.drawValuesEnabled = true
        set1.valueFormatter = self as IValueFormatter
        set1.valueTextColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
        set1.valueFont = UIFont.systemFont(ofSize: 11.0)
        let data = BarChartData.init(dataSet: set1)
        chartView.data  = data
        
        chartView.xAxis.valueFormatter = self as IAxisValueFormatter
        chartView.xAxis.labelCount = datas.count
        chartView.fitBars = true
        chartView.barData?.barWidth = 0.3
        
        chartView.setNeedsLayout()
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var lineOneView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCMain
        return view
    }()

    lazy var lineTwoView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()

    lazy var lineThreeView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()

    lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.MCWords
        label.text = "XXX"
        label.font = UIFont.boldSystemFont(ofSize: 17)
        return label
    }()
    
    lazy var leftView: MCReportFormsItem_statisticsView = {
        let view = MCReportFormsItem_statisticsView()
        view.topLabel.text = "昨日订单量"
        return view
    }()
    
    lazy var centerView: MCReportFormsItem_statisticsView = {
        let view = MCReportFormsItem_statisticsView()
        view.topLabel.text = "昨日净增订单量"
        return view
    }()
    
    lazy var rightView: MCReportFormsItem_statisticsView = {
        let view = MCReportFormsItem_statisticsView()
        view.topLabel.text = "本月订单量"
        view.lineTwoView.isHidden = true
        return view
    }()

    lazy var chartView: BarChartView = {
        let view = BarChartView()
        view.delegate = self
        view.chartDescription?.text = ""
        
        
        // 设置类型视图
        //设置类型试图的对齐方式，右上角
        let legend = view.legend
        legend.enabled = true
        legend.horizontalAlignment = .right
        legend.verticalAlignment = .top
        legend.orientation = .horizontal
        legend.textColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
        legend.font = UIFont.systemFont(ofSize: 11.0)
        
        
        
        
        // 设置X轴属性
        //设置x轴的属性，颜色，字体大小等
        let xAxis = view.xAxis
        xAxis.labelPosition = .bottom
        xAxis.drawGridLinesEnabled = false
        xAxis.gridColor = UIColor.init(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)
        xAxis.axisLineColor = UIColor.init(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)
        xAxis.labelTextColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
        xAxis.labelFont = UIFont.systemFont(ofSize: 11.0)
        
        
        
        // 设置Y轴属性
        //设置y轴的属性,颜色，字体大小等
        view.leftAxis.drawGridLinesEnabled = true
        view.leftAxis.gridColor = UIColor.init(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0)
        view.leftAxis.axisLineColor = UIColor.clear
        view.leftAxis.labelTextColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0)
        view.leftAxis.valueFormatter = self
        view.rightAxis.drawGridLinesEnabled = false
        view.rightAxis.enabled = false
        view.leftAxis.axisMinimum = 0.0
        
        
        return view
    }()
}


extension MCReportFormsItemView : ChartViewDelegate {
}


class MCReportFormsItem_statisticsView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(topLabel)
        topLabel.snp.remakeConstraints { (make) ->Void in
            make.bottom.equalTo(self.snp.centerY).offset(-2)
            make.centerX.equalTo(self)
        }
        
        self.addSubview(bottomLabel)
        bottomLabel.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(self.snp.centerY).offset(2)
            make.centerX.equalTo(self)
        }

        self.addSubview(lineOneView)
        lineOneView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(self)
            make.height.equalTo(1)
        }
        
        self.addSubview(lineThreeView)
        lineThreeView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(7)
        }
        
        self.addSubview(lineTwoView)
        lineTwoView.snp.remakeConstraints { (make) ->Void in
            make.right.centerY.equalTo(self)
            make.width.equalTo(1)
            make.height.equalTo(44)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var lineOneView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    public lazy var lineTwoView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    private lazy var lineThreeView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    
    
    lazy var topLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.MCGray_middle
        label.text = "XXX"
        label.font = MCFont(12)
        return label
    }()
    
    lazy var bottomLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.MCWords
        label.text = "X"
        label.font = MCFont(12)
        return label
    }()
}



extension MCReportFormsItemView:  IAxisValueFormatter, IValueFormatter{
    
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        
        if axis == chartView.leftAxis {  //y轴
            if value == Double(Int(value)) {
                if value >= 1000 {
                    var y_value = String(format: "%.3f", value/1000)
                    if y_value.hasSuffix("0") {
                        y_value.remove(at: y_value.index(before: y_value.endIndex))
                    }
                    if y_value.hasSuffix("0") {
                        y_value.remove(at: y_value.index(before: y_value.endIndex))
                    }
                    
                    if y_value.hasSuffix("0") {
                        y_value.remove(at: y_value.index(before: y_value.endIndex))
                    }
                    
                    if y_value.hasSuffix(".") {
                        y_value.remove(at: y_value.index(before: y_value.endIndex))
                    }
                    
                    return y_value+"k"
                }else {
                    return "\(Int(value))"
                }
            }else {
                return ""
            }
        }else {    //x轴
            if let titles = titles {

                if Int(value) <= titles.count-1 {
                    return titles[Int(value)]
                }
            }
            return ""
        }
        
    }
    
    
    
    func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
        
        if promptLabel.text == "订单量" {
             return String(Int(value))
        } else {
            return "\(value)"
        }
    }
}










